import { Store } from 'iflux2';
import FilterActor from './actor/filter-actor';
import * as types from './constants/actionTypes';
import webApi from './webapi';

export default class AppStore extends Store {
  bindActor() {
    return [new FilterActor()];
  }

  constructor(props) {
    super(props);
  }

  addCondition = value => {
    this.dispatch(types.ADD_CONDITION, value);
    this.fetchFansCount();
  };

  selectItem = (value,fetchFlag=true) => {
    this.dispatch(types.SELECT_ITEM, value);
	  fetchFlag && this.fetchFansCount();
  };

  fetchFans = async () => {
    let { last } = this.state().toJS();
    let fansList = await webApi.fetchFans(last);
    let touser = fansList && fansList.length
      ? fansList.map((fans, index) => {
          return fans.openId;
        })
      : [];
    this.dispatch(types.FETCH_FANS, touser);
  };
  
	fetchFansCount = async ()=>{
		let { last } = this.state().toJS();
		let totalCount = await webApi.fetchFansCount(last);
		this.dispatch(types.FETCH_FANS_COUNT,totalCount||0)
  }
}
